home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER4 / COLS.C < prev    next >
C/C++ Source or Header  |  1996-03-21  |  10KB  |  285 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "cols.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLColumns()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static HENV  hEnv  = NULL;
  111. static HDBC  hDBC  = NULL;
  112. static HWND  hList = NULL;
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE :
  117.               {
  118.                  RETCODE retCode;
  119.  
  120.                  // Allocate Environment and Connection.
  121.                  //.....................................
  122.                  SQLAllocEnv( &hEnv );
  123.                  SQLAllocConnect( hEnv, &hDBC );
  124.  
  125.                  // Connect to the data source
  126.                  //...........................
  127.                  retCode = SQLConnect( hDBC, 
  128.                              "Test Data Source", SQL_NTS,
  129.                              "DBA",              SQL_NTS,  
  130.                              "SQL",              SQL_NTS );
  131.  
  132.                  if ( retCode != SQL_SUCCESS )
  133.                  {
  134.                     SQLFreeConnect( hDBC );
  135.                     SQLFreeEnv( hEnv );
  136.                     return( -1 );
  137.                  }
  138.  
  139.                  if ( retCode == SQL_SUCCESS )
  140.                  {
  141.                     int nTabSize = 60;
  142.  
  143.                     hList = CreateWindow( "LISTBOX", "",    
  144.                                LBS_NOTIFY | LBS_USETABSTOPS |
  145.                                LBS_NOINTEGRALHEIGHT  | LBS_SORT | 
  146.                                WS_BORDER  | WS_CHILD | 
  147.                                WS_VISIBLE | WS_VSCROLL, 
  148.                                0, 0, 0, 0, 
  149.                                hWnd, (HMENU)101,
  150.                                hInst, NULL );
  151.                     
  152.                     SendMessage( hList, LB_SETTABSTOPS, 
  153.                                  1, (LPARAM)&nTabSize );
  154.                  }
  155.               }
  156.               break;
  157.  
  158.       case WM_SIZE :
  159.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  160.                                        HIWORD( lParam ), TRUE );
  161.               break; 
  162.               
  163.       case WM_COMMAND :
  164.               switch( LOWORD( wParam ) )
  165.               {
  166.                  case IDM_TEST :
  167.                         {
  168.                            HSTMT   hStmt;
  169.                            UCHAR   szColumnName[128];
  170.                            UCHAR   szTypeName  [ 32];
  171.                            UCHAR   szLBStr     [128];
  172.                            SDWORD  dwTypeLen;
  173.                            RETCODE retCode;
  174.  
  175.                            // Allocate a statement handle
  176.                            // to receive the result set.
  177.                            // ...........................
  178.                            SQLAllocStmt( hDBC, &hStmt );
  179.  
  180.                            // Call SQLColumns() to determine the 
  181.                            // columns within the 'department' table.
  182.                            // ......................................
  183.                            retCode = SQLColumns( hStmt, 
  184.                                         NULL,         SQL_NTS, 
  185.                                         NULL,         SQL_NTS, 
  186.                                         "department", SQL_NTS, 
  187.                                         "%",          SQL_NTS );
  188.  
  189.                            if( retCode == SQL_SUCCESS || 
  190.                                retCode == SQL_SUCCESS_WITH_INFO )
  191.                            {
  192.                               // Bind a column to the COLUMN_NAME
  193.                               // column in the result set 
  194.                               // generated by SQLColumns().
  195.                               // ................................
  196.                               SQLBindCol( hStmt, 4, SQL_C_CHAR, szColumnName,
  197.                                           sizeof( szColumnName ), &dwTypeLen );
  198.  
  199.                               // Bind a column to the TYPE_NAME
  200.                               // column in the result set 
  201.                               // generated by SQLColumns().
  202.                               // ..............................
  203.                               SQLBindCol( hStmt, 6, SQL_C_CHAR, szTypeName,
  204.                                           sizeof( szTypeName ), &dwTypeLen );
  205.  
  206.                               // Fetch all the records and
  207.                               // display them in the list box.
  208.                               //..............................
  209.                               retCode = SQLFetch( hStmt );
  210.  
  211.                               while ( retCode == SQL_SUCCESS )
  212.                               {
  213.                                  // Set string contents to be  
  214.                                  // column name and type name.
  215.                                  // ..........................
  216.                                  szLBStr[0] = '\0';
  217.                                  strcat( szLBStr, szColumnName ); 
  218.                                  strcat( szLBStr, "\t" );
  219.                                  strcat( szLBStr, szTypeName );
  220.  
  221.                                  SendMessage( hList, LB_ADDSTRING, 
  222.                                               0, (LPARAM)szLBStr );
  223.  
  224.                                  retCode = SQLFetch( hStmt );
  225.                               }
  226.                            }
  227.  
  228.                            SQLFreeStmt( hStmt, SQL_DROP );
  229.                         }
  230.                         break;
  231.  
  232.                  case IDM_ABOUT :
  233.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  234.                         break;
  235.  
  236.                  case IDM_EXIT :
  237.                         DestroyWindow( hWnd );
  238.                         break;
  239.               }
  240.               break;
  241.       
  242.       case WM_DESTROY :
  243.               PostQuitMessage(0);
  244.  
  245.               // Disconnect Environment.
  246.               //........................
  247.               SQLDisconnect( hDBC );
  248.  
  249.               // Free Connection and Environment.
  250.               //.................................
  251.               SQLFreeConnect( hDBC );
  252.               SQLFreeEnv( hEnv );
  253.               break;
  254.  
  255.       default :
  256.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  257.    }
  258.  
  259.    return( 0L );
  260. }
  261.  
  262.  
  263. LRESULT CALLBACK About( HWND hDlg,           
  264.                         UINT message,        
  265.                         WPARAM wParam,       
  266.                         LPARAM lParam)
  267. {
  268.    switch (message) 
  269.    {
  270.        case WM_INITDIALOG: 
  271.                return (TRUE);
  272.  
  273.        case WM_COMMAND:                              
  274.                if (   LOWORD(wParam) == IDOK         
  275.                    || LOWORD(wParam) == IDCANCEL)    
  276.                {
  277.                        EndDialog(hDlg, TRUE);        
  278.                        return (TRUE);
  279.                }
  280.                break;
  281.    }
  282.  
  283.    return (FALSE); 
  284. }
  285.